/**
 * @author Daniele Cangemi
 */

			/*  
			 *  Pagina di amminstrazione delle notizie in home page
			 */
			function aggiungiNews(){
				titolo = document.getElementById('txtTitolo').value;
				news = tinyMCE.activeEditor.getContent(); // document.getElementById('txtNews').value;
				doPostBack('news', 'btnAddNews=Aggiungi&txtTitolo='+titolo+'&txtNews='+news);
			}
			
			function delNews(idNews){
				doPostBack('news', 'btnElimina=Elimina&hdIdNews='+idNews);
			}
			
			/*  
			 *  Pagina di amminstrazione degli utenti
			 */
			function showUserDetails(idUser){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvUserDetail").innerHTML=xmlhttp.responseText;
				    }else{
				    	document.getElementById("dvUserDetail").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    	//document.getElementById("dvUserDetail").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:50px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				document.getElementById("dvUserDetail").style.display="";
				
				//$('#dvUserDetail').dialog("open");
				doPostBack('utenti','user='+idUser, f);
				
			}
			
			function closeDetails(div){
				dv = document.getElementById(div);
				if(dv!=null) dv.style.display='none';
				//$("#dvUserDetail").dialog("close");
			}
			
			function saveUserDetails(){
			/* salva le eventuali modifiche all'utente */
				id = document.getElementById('hdId').value;
				nome = document.getElementById('txtNome').value;
				cognome = document.getElementById('txtCognome').value;
				alias = document.getElementById('txtAlias').value;
				email = document.getElementById('txtEmail').value;
				abilitato = document.getElementById('cmbAbilitato').value;
				ruolo = document.getElementById('cmbRuolo').value;
				doPostBack('utenti', 'btnSaveDetails=Salva&hdId=' + id + '&txtNome=' + nome + '&txtCognome=' + cognome +
				'&txtAlias=' + alias + '&txtEmail=' + email + '&cmbAbilitato=' + abilitato + '&cmbRuolo=' + ruolo);
			}
			
			function newUser(){
			/* crea un nuovo utente */
				nome = document.getElementById('txtNome').value;
				cognome = document.getElementById('txtCognome').value;
				alias = document.getElementById('txtAlias').value;
				email = document.getElementById('txtEmail').value;
				abilitato = document.getElementById('cmbAbilitato').value;
				ruolo = document.getElementById('cmbRuolo').value;
				doPostBack('utenti', 'btnSaveDetails=Nuovo&hdId=-1&txtNome=' + nome + '&txtCognome=' + cognome +
				'&txtAlias=' + alias + '&txtEmail=' + email + '&cmbAbilitato=' + abilitato + '&cmbRuolo=' + ruolo);
			}
			
			
			/*  
			 *  Pagina di amminstrazione delle leghe
			 */
						
			function showLeagueDetails(legaId){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvLeagueDetail").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvLeagueDetail").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				document.getElementById("dvLeagueDetail").style.display="";
				
				//$('#dvUserDetail').dialog("open");
				doPostBack('leghe','cmd=dettagli&lega='+legaId, f);
				
			}
			
			
			function saveLeagueDetails(){
			/* salva le eventuali modifiche alla lega */
				id = document.getElementById('hdId').value;
				nome = document.getElementById('txtNome').value;
				stagione = document.getElementById('txtStagione').value;
				abilitato = document.getElementById('cmbAbilitato').value;
				competizione = document.getElementById('cmbTipoLega').value;
				doPostBack('leghe', 'btnSaveDetails=Salva&hdId=' + id + '&txtNome=' + nome + '&txtStagione=' + stagione +
				'&cmbAbilitato=' + abilitato+"&cmbTipoLega=" + competizione);
			}
			
			function newLeague(){
			/* crea una nuova lega */
				id = document.getElementById('hdId').value;
				nome = document.getElementById('txtNome').value;
				stagione = document.getElementById('txtStagione').value;
				abilitato = document.getElementById('cmbAbilitato').value;
				competizione = document.getElementById('cmbTipoLega').value;
				doPostBack('leghe', 'btnSaveDetails=Nuovo&hdId=-1&txtNome=' + nome + '&txtStagione=' + stagione +
				'&cmbAbilitato=' + abilitato+"&cmbTipoLega=" + competizione);
			}
			
			function showLeagueTeams(legaId){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvLeagueTeams").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvLeagueTeams").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				document.getElementById("dvLeagueTeams").style.display="";
				
				//$('#dvUserDetail').dialog("open");
				doPostBack('leghe','cmd=squadre&lega='+legaId, f);
			}
			
			function loadTeamOfLeague(){
				var cmbLeghe = document.getElementById('cmbLeghe');
				var legaId = cmbLeghe.options[cmbLeghe.selectedIndex].value;
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("contentAdmin").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("contentAdmin").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				doPostBack('squadre','cmbLeghe='+legaId, f);
				
			}
			
			function addTeamToLeague(){
			/* Aggiunge una squadra alla lega */
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvLeagueTeams").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvLeagueTeams").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				cmbSquadre = document.getElementById('cmbNoSquadre');
				legaId = document.getElementById('hLeagueId').value;
				teamId = cmbSquadre.options[cmbSquadre.selectedIndex].value;
				doPostBack('leghe','cmd=aggiungiSquadra&lega=' + legaId + '&squadra=' + teamId, f);		
			}
			
			function removeTeamToLeague(teamId, nomeSquadra){
			/* Aggiunge una squadra alla lega */
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvLeagueTeams").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvLeagueTeams").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				var res = confirm("Sei sicuro di voler eliminare la squadra " + nomeSquadra + " da questa lega?")
				if(res){
					legaId = document.getElementById('hLeagueId').value;
					doPostBack('leghe','cmd=eliminaSquadra&lega=' + legaId + '&squadra=' + teamId, f);
				}				
			}
			
			function makeCalendar(legaId){
			/*Crea il calendario per questa lega*/
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvLeagueTeams").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvLeagueTeams").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				var res = confirm("Generando il calendario non sara' piu' possibile modificare le squadre della lega.\nContinuare?")
				if(res){
					doPostBack('leghe','cmd=generaCalendario&lega=' + legaId, f);
				}
			}
			
			/*
			 * Pagina delle squadre 
			 * */
		function showTeamDetails(teamId){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvTeamDetail").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvTeamDetail").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				document.getElementById("dvTeamDetail").style.display="";
				
				//$('#dvUserDetail').dialog("open");
				doPostBack('squadre','squadra='+teamId, f);
		}
			
			
			function saveTeamDetails(){
			/* salva le eventuali modifiche alla lega */
				id = document.getElementById('hdId').value;
				nome = document.getElementById('txtNome').value;
				budget = document.getElementById('txtBudget').value;
				logo = document.getElementById('txtLogo').value;
				allenatore = document.getElementById('cmbAllenatore').value;
				doPostBack('squadre', 'btnSaveDetails=Salva&hdId=' + id + '&txtNome=' + nome + '&txtBudget=' + budget +
				'&txtLogo=' + logo+"&cmbAllenatore=" + allenatore);
			}
			
			function newTeam(){
			/* crea una nuova lega */
				nome = document.getElementById('txtNome').value;
				budget = document.getElementById('txtBudget').value;
				logo = document.getElementById('txtLogo').value;
				allenatore = document.getElementById('cmbAllenatore').value;
				doPostBack('squadre', 'btnSaveDetails=Salva&hdId=-1&txtNome=' + nome + '&txtBudget=' + budget +
				'&txtLogo=' + logo+"&cmbAllenatore=" + allenatore);
			}
		
		/**
		 * Pagina delle rose
		 * */
			function showTeamPlayers(teamId, stagione){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvTeamPlayers").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvTeamPlayers").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				document.getElementById("dvTeamPlayers").style.display="";
				
				//$('#dvUserDetail').dialog("open");
				doPostBack('rosa','teamId='+teamId+"&stagione="+stagione, f);
			}
			
			function changePlayerState(combo, playerId, teamId){
				f = function(){}
				var status = combo.options[combo.selectedIndex].value;
				doPostBack('rosa','cmd=changeStatus&playerId='+playerId+"&teamId="+teamId+"&status="+status, f);
			}
			
			function addPlayerToTeam(){
			/* aggiunge il calciatore selezionato alla squadra corrente */
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvTeamPlayers").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvTeamPlayers").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				cmbPlayers = document.getElementById('cmbFreePlayer');
				if(cmbPlayers.selectedIndex < 0) return;
				playerId = cmbPlayers.options[cmbPlayers.selectedIndex].value;
				teamId = document.getElementById('hTeamId').value;
				costo = document.getElementById('txtCosto').value;
				btnAdd = document.getElementById('btnAddPlayer');
				if(costo != '0' && costo!='0'){
					doPostBack('rosa', 'cmd=addPlayer&playerId=' + playerId + '&teamId=' + teamId + '&costo=' + costo, f);
				}
			}
			
			function txtCosto_changed(){
				var txt = document.getElementById('txtCosto');
				var btnAdd = document.getElementById('btnAddPlayer');
				btnAdd.disabled = (txt.value == '0' || txt.value=='' || txt.value.search(/\d+(.\d+)?/)<0)
				btnAdd.style.color = btnAdd.disabled ? 'gray' : 'blue';
			}
		
			/*
			 * Funzione per l'aperta dei menu dalla pagina principale di amministrazione 
			 * */
			function openMenu(strMenu)
			{
			
				if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp=new XMLHttpRequest();
				}else {// code for IE6, IE5
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				  
				xmlhttp.onreadystatechange=function()
				{
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("contentAdmin").innerHTML=xmlhttp.responseText;
						initTiny();
				    }else{
				    	document.getElementById("contentAdmin").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				xmlhttp.open("GET",strMenu + ".php",true);
				//document.write("/admin/" + strMenu + ".php");
				xmlhttp.send();
			}
			
			function initTiny(){
				tinyMCE.init({
						        // General options
						        mode : "exact",
						        elements : "txtNews",
						        theme : "advanced",
						        plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
						
						        // Theme options
						        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
						        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,|,insertdate,inserttime,preview,|,forecolor,backcolor",
						        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
						        theme_advanced_toolbar_location : "top",
						        theme_advanced_toolbar_align : "left",
						        theme_advanced_statusbar_location : "bottom",
						        theme_advanced_resizing : true,
						
						        // Skin options
						        skin : "o2k7",
						        skin_variant : "silver",
						
						        // Example content CSS (should be your site CSS)
						        content_css : "css/example.css",
						
						        // Drop lists for link/image/media/template dialogs
						        template_external_list_url : "js/template_list.js",
						        external_link_list_url : "js/link_list.js",
						        external_image_list_url : "js/image_list.js",
						        media_external_list_url : "js/media_list.js",
						
						        // Replace values for the template plugin
						        template_replace_values : {
						                username : "Some User",
						                staffid : "991234"
						        }
						});
			}
			
			function doPostBack(page, postbackargs, postBackFunc)
			{
				postbackargs="IsPostBack=true&"+postbackargs;
				if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp=new XMLHttpRequest();
				}else {// code for IE6, IE5
					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
				 
				xmlhttp.onreadystatechange= postBackFunc != null ? postBackFunc : function()
				{
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("contentAdmin").innerHTML=xmlhttp.responseText;
						/*tinyMCE.init({ 
						        mode : 'textareas', 
						        theme : 'simple'   //(n.b. no trailing comma, this will be critical as you experiment later) 
						});*/ 
				    }else{
				    	document.getElementById("contentAdmin").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				xmlhttp.open("POST",page + ".php",true);
				xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlhttp.setRequestHeader("Content-length", postbackargs.length);
				xmlhttp.setRequestHeader("Connection", "close");
				//document.write("/admin/" + strMenu + ".php");
				xmlhttp.send(postbackargs);
			}

		
		
			function calcolaRisultati(){
				var giornata = document.getElementById("cmbGiornate").value;
				doPostBack('calcolaRisultati', 'btnCalcola=calcola&cmbGiornate='+giornata);
			}
			
			/*
			 Pagina di amministrazione del DB
			 * */
			function execQuery(){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvQueryResult").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvQueryResult").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				var query = document.getElementById('txtQuery').value;
				doPostBack('sql', 'btnEsegui=btnEsegui&txtQuery='+ query, f);
			}
 
			function execNonQuery(){
				f = function(){
					if (xmlhttp.readyState==4 && xmlhttp.status==200)  {
				    	document.getElementById("dvQueryResult").innerHTML=xmlhttp.responseText;
				    }else{
				    	// in attesa mostra la gif animata
				    	document.getElementById("dvQueryResult").innerHTML="<div style='width:100%;height:200px;text-align:center;padding-top:100px;'><img src= '../images/wait.gif' align='center'  style='margin-left:50px;margin-top:50px;'  alt='connessione al server in corso...' /></div>";
				    }
				}
				var query = document.getElementById('txtQuery').value;
				doPostBack('sql', 'btnAggiorna=btnAggiorna&txtQuery='+ query, f);
			}
			
			function viewSchema(){
				document.getElementById('dvSchema').style.display='';
			}

			IE = document.all ? true : false
			mouseX = 0;
			mouseY = 0;
			LF_BUTTON = 0;
			canMoveDiv = false;
			function initMove(){
				if (!IE) {
					document.addEventListener(Event.MOUSEMOVE, getMouseXY, true);
				}else{
					LF_BUTTON = 1;
				}
				document.onmousemove = getMouseXY;
			}
			
			function moveDiv(e){
				canMoveDiv = true;
			}
			
			function stopMoveDiv(e){
				canMoveDiv = false;
			}
			
			function movingDiv(e){
				if(canMoveDiv){
					var div = document.getElementById("dvSchema");
					div.style.top = (mouseY - 20) + "px";
					div.style.left = (mouseX - 20) + "px";
				}
			}
				
			function getMouseXY(e) {
			// Main function to retrieve mouse x-y pos.s
				if (IE) { // grab the x-y pos.s if browser is IE
				mouseX = event.clientX + document.body.scrollLeft;
				mouseY = event.clientY + document.body.scrollTop;
				} else { // grab the x-y pos.s if browser is NS
				mouseX = e.pageX;
				mouseY = e.pageY;
				}
				// catch possible negative values in NS4
				if (mouseX < 0) { mouseX = 0 }
				if (mouseY < 0) { mouseY = 0 }
			}
			
			function txtFindPlayer_changed(e){
				var cmb = document.getElementById('cmbFreePlayer');
				var txt = document.getElementById('txtFindPlayer');
			    var keynum;
			    if(window.event){ // IE
			        keynum = e.keyCode;
			    }else
			    if(e.which){ // Netscape/Firefox/Opera
			        keynum = e.which;
			    }
			    if(keynum==13){ //enter
			    	txt.value = cmb.options[cmb.selectedIndex].text;
			    	cmb.style.display='none';
			    	return;
			    }else if(keynum==27){ //ESC
			    	cmb.style.display='none';
			    	cmb.selectedIndex = -1;
			    }

				var value = txt.value;
				var players = cmb.options;
				cmb.style.display='';
				cmb.style.top=txt.style.top  + txt.style.left;
				for(var i=0; i<players.length; i++){
					var n = players[i].text.search(value.toUpperCase()); 
					if(n>-1){
						cmb.selectedIndex=i;
						return;
					}
				} //end for
			}
			
			function cmbFreePlayer_click(){
				var txt = document.getElementById('txtFindPlayer');
				var cmb = document.getElementById('cmbFreePlayer');
				txt.value = cmb.options[cmb.selectedIndex].text;
				cmb.style.display='none';
			}
			
			function hideFreePlayerList(){
				/*var cmb = document.getElementById('cmbFreePlayer');
				if(document.activeElement.id != 'cmbFreePlayer')
					cmb.style.display='none';*/
			}
				
